/**
 * @author 徐楠
 * @date 2022/3/28 23:45
 * @version 1.0
 */

package com.xunan.likou;

public class RotationFunction {
    public static void main(String[] args) {
        int nums[] = {4, 3, 2, 6};
        int result = maxRotateFunction(nums);
        int resultPlus = maxRotateFunctionPlus(nums);
        System.out.println(result);
        System.out.println(resultPlus);
    }

    public static int maxRotateFunction(int[] nums) {
        int result = 0;
        int sum = 0;
        int length = nums.length;
        if (length == 1) {
            return 0;
        }
        if (length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                sum += nums[(j + i) % length] * j;
            }
            System.out.println("sum " + sum);
            result = Math.max(result, sum);
            sum = 0;
        }
        return result;
    }

    public static int maxRotateFunctionPlus(int[] nums) {
        int sum = 0;
        int function = 0;
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            sum += nums[i];
            function += i * nums[i];
        }
        int maxFunction = function;
        for (int i = 1; i < length; i++) {
            function = function + sum - length * nums[length - i];
            maxFunction = Math.max(maxFunction, function);
        }
        return maxFunction;
    }

}
